package com.lisp.springframework.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import com.google.common.base.Joiner;

public class BindResultUtil {

    public static List<String> defaultMessages(BindingResult br) {
        
        if(br.hasErrors()) {
            List<FieldError> fieldErrors = br.getFieldErrors();
            ArrayList<String> ret = new ArrayList<>(fieldErrors.size());
            for(FieldError fe : fieldErrors) {
                ret.add(fe.getDefaultMessage());
            }
            return ret;
        } else {
            return Collections.emptyList();
        }
    }
    
    public static String joinedDefaultMessages(BindingResult br) {
        return Joiner.on(";").join(defaultMessages(br));
    }
}
